package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.calculator.general.CalViewModel;
import com.stark.calculator.general.TextBean;

import flc.ast.R;
import flc.ast.activity.HouseCalculatorActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.activity.TaxCalculatorActivity;
import flc.ast.activity.UnitCalculatorActivity;
import flc.ast.databinding.FragmentCalculateBinding;
import flc.ast.utils.TypefaceUtil;
import stark.common.basic.base.BaseFragment;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.event.usersys.IUserSysEvent;
import stark.common.basic.event.usersys.UserSysEventProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CalculateFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CalculateFragment extends BaseFragment<CalViewModel, FragmentCalculateBinding> {


    @Override
    protected CalViewModel initViewModel() {
        return new ViewModelProvider(this).get(CalViewModel.class);
    }

    @Override
    protected void showError(Object obj) {

    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_calculate;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.rlHouseCalculate.setOnClickListener(this);
        mDataBinding.rlTaxCalculate.setOnClickListener(this);
        mDataBinding.rlUnitCalculate.setOnClickListener(this);


        mDataBinding.ivCalculateClear.setOnClickListener(this);
        mDataBinding.ivCalculateBf.setOnClickListener(this);
        mDataBinding.ivCalculateChu.setOnClickListener(this);
        mDataBinding.ivCalculateDelete.setOnClickListener(this);

        mDataBinding.ivCalculateNum1.setOnClickListener(this);
        mDataBinding.ivCalculateNum2.setOnClickListener(this);
        mDataBinding.ivCalculateNum3.setOnClickListener(this);
        mDataBinding.ivCalculateCheng.setOnClickListener(this);

        mDataBinding.ivCalculateNum4.setOnClickListener(this);
        mDataBinding.ivCalculateNum5.setOnClickListener(this);
        mDataBinding.ivCalculateNum6.setOnClickListener(this);
        mDataBinding.ivCalculateJian.setOnClickListener(this);

        mDataBinding.ivCalculateNum7.setOnClickListener(this);
        mDataBinding.ivCalculateNum8.setOnClickListener(this);
        mDataBinding.ivCalculateNum9.setOnClickListener(this);
        mDataBinding.ivCalculateJia.setOnClickListener(this);

        mDataBinding.ivCalculateChange.setOnClickListener(this);
        mDataBinding.ivCalculateNum0.setOnClickListener(this);
        mDataBinding.ivCalculateDian.setOnClickListener(this);
        mDataBinding.ivCalculateDy.setOnClickListener(this);
        TypefaceUtil.setViewTypefaceBySdc(mDataBinding.tvCalculateInput, mDataBinding.tvCalculateResult);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.rlHouseCalculate://房贷计算
                UserSysEventProxy.getInstance().payEvent(getActivity(), new IUserSysEvent.IPayEventCallback() {
                    @Override
                    public void onPayOk(boolean isRealOk) {
                        if (isRealOk) {
                            startActivity(HouseCalculatorActivity.class);
                        } else {
                            ToastUtils.showShort(getString(R.string.not_vip));
                        }
                    }
                });
                break;
            case R.id.rlTaxCalculate://个税计算
                startActivity(TaxCalculatorActivity.class);
                break;
            case R.id.rlUnitCalculate://单位计算
                startActivity(UnitCalculatorActivity.class);
                break;
            case R.id.ivCalculateDelete:
                mViewModel.handleClickDelete();
                break;
            case R.id.ivCalculateChange:
            case R.id.ivCalculateClear:
                mViewModel.clears();
                break;
            case R.id.ivCalculateChu:
                mViewModel.handleClickAsmd("÷");
                break;
            case R.id.ivCalculateCheng:
                mViewModel.handleClickAsmd("×");
                break;
            case R.id.ivCalculateNum7:
                mViewModel.handleClickNum("7");
                break;
            case R.id.ivCalculateNum8:
                mViewModel.handleClickNum("8");
                break;
            case R.id.ivCalculateNum9:
                mViewModel.handleClickNum("9");
                break;
            case R.id.ivCalculateJian:
                mViewModel.handleClickAsmd("-");
                break;
            case R.id.ivCalculateNum4:
                mViewModel.handleClickNum("4");
                break;
            case R.id.ivCalculateNum5:
                mViewModel.handleClickNum("5");
                break;
            case R.id.ivCalculateNum6:
                mViewModel.handleClickNum("6");
                break;
            case R.id.ivCalculateJia:
                mViewModel.handleClickAsmd("+");
                break;
            case R.id.ivCalculateNum0:
                mViewModel.handleClickNum("0");
                break;
            case R.id.ivCalculateNum1:
                mViewModel.handleClickNum("1");
                break;
            case R.id.ivCalculateNum2:
                mViewModel.handleClickNum("2");
                break;
            case R.id.ivCalculateNum3:
                mViewModel.handleClickNum("3");
                break;
            case R.id.ivCalculateDian:
                mViewModel.handleClickPoint(".");
                break;
            case R.id.ivCalculateDy:
                mViewModel.handleClickEqual();
                break;
        }
    }

    @Override
    protected void initData() {
        // 中间计算的步骤
        mViewModel.getFuncText().observe(this, textBean -> {
            updateViewText(mDataBinding.tvCalculateInput, textBean);
        });
        // 最后得到的结果
        mViewModel.getEqualText().observe(this, textBean -> {
            updateViewText(mDataBinding.tvCalculateResult, textBean);
        });
    }

    private void updateViewText(TextView textView, TextBean textBean) {
        if (textView == null || textBean == null) {
            return;
        }
        textView.setText(textBean.getText());
//        if (textBean.getTextSize() > 0) {
//            textView.setTextSize(textBean.getTextSize());
//        }
    }

}